package rolesystem.middle.managers;

import java.util.ArrayList;

import rolesystem.core.RoleEvent;
import rolesystem.core.RoleException;
import rolesystem.core.RoleRegistration;
import rolesystem.middle.utilityclasses.EventData;
import rolesystem.middle.utilityclasses.MessagePayload;
import rolesystem.roles.KnownEvent;

public class CommunicationManager
{
	private ArrayList<EventData> messagesReceived;
	
	public CommunicationManager()
	{
		messagesReceived=new ArrayList<EventData>();
	}
	
	public void clearAllEvents(RoleRegistration registration)
	{
		try
		{
			while(registration.listenNoWait()!=null) {}
		} catch (RoleException e)
		{
			e.printStackTrace();
		}
		
		messagesReceived.clear();
	}
	
	public boolean receiveEvent(RoleRegistration registration) 
	{
		RoleEvent event;
		boolean eventFound=false;
		
		do
		{
			try
			{
				event = registration.listenNoWait();
			} catch (RoleException e)
			{
				event=null;
				e.printStackTrace();
			}
			
			if(event!=null)
			{
				messagesReceived.add(new EventData(event.getName(), event.getSender(), event.getSenderRole() ,(MessagePayload)event.getContent()));
				
				eventFound=true;
			}			
			
		}while(event!=null);
		
		return eventFound;
	}
	
	public void receiveEventTimeout(RoleRegistration registration,long reptoManager)
	{
		RoleEvent event=null;
		
		try
		{
			event = registration.listen(reptoManager);
		} catch (RoleException e)
		{
			e.printStackTrace();
		}
		
		if(event==null)
			return;
		
		messagesReceived.add(new EventData(event.getName(), event.getSender(), event.getSenderRole() ,(MessagePayload)event.getContent()));
	}
	
	public void receiveEventBlocking(RoleRegistration registration)
	{
		RoleEvent event=null;
		
		try
		{
			event = registration.listen();
		} catch (RoleException e)
		{
			e.printStackTrace();
		}
		
		if(event==null)
			return;
		
		messagesReceived.add(new EventData(event.getName(), event.getSender(), event.getSenderRole() ,(MessagePayload)event.getContent()));
	}
	
	public EventData requestEvent(KnownEvent knEvent)
	{
		return requestEvent(knEvent.getName(), knEvent.getSenderRole());
	}
	
	public EventData requestEvent(String EventName, String senderRole)
	{
		for(int i=0;i<messagesReceived.size();i++)
		{
			if(messagesReceived.get(i).getEventName().equals(EventName) && messagesReceived.get(i).getSenderRole().equals(senderRole))
				return messagesReceived.remove(i);
		}	
		
		return null;
	}
	
	public MessagePayload requestEventPayload(KnownEvent knEvent)
	{
		return requestEventPayload(knEvent.getName(), knEvent.getSenderRole());
	}
	
	public MessagePayload requestEventPayload(String EventName, String senderRole)
	{
		for(int i=0;i<messagesReceived.size();i++)
		{
			if(messagesReceived.get(i).getEventName().equals(EventName) && messagesReceived.get(i).getSenderRole().equals(senderRole))
				return messagesReceived.remove(i).getContent();
		}	
		
		return null;
	}
	
	public boolean requestEventExist(KnownEvent knEvent)
	{
		return requestEventExist(knEvent.getName(), knEvent.getSenderRole());
	}
	
	public boolean requestEventExist(String EventName, String senderRole)
	{
		for(int i=0;i<messagesReceived.size();i++)
		{
			if(messagesReceived.get(i).getEventName().equals(EventName) && messagesReceived.get(i).getSenderRole().equals(senderRole))
			{
				messagesReceived.remove(i);
				return true;
			}	
		}	
		
		return false;
	}
	
	public boolean checkEventExist(KnownEvent knEvent)
	{
		return checkEventExist(knEvent.getName(), knEvent.getSenderRole());
	}
	
	public boolean checkEventExist(String EventName, String senderRole)
	{
		for(int i=0;i<messagesReceived.size();i++)
		{
			if(messagesReceived.get(i).getEventName().equals(EventName) && messagesReceived.get(i).getSenderRole().equals(senderRole))
				return true;
		}	
		
		return false;
	}
}



